package com.beerq.view.me;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beerq.R;
import com.beerq.util.BasicHttp;
import com.beerq.util.Constants;
import com.beerq.util.ViewPaperUtil;
import com.beerq.util.WebViewUtil;
import com.beerq.view.BaseActivity;
import com.beerq.view.ProductJsManager;
import com.beerq.view.friends.FriendsManager;
import com.beerq.view.pulltorefresh.library.PullToRefreshListView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;


import java.util.ArrayList;
import java.util.List;

import static com.alibaba.fastjson.JSON.parseObject;

public class MyCollectActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager mViewPager;
    private List<View> mListViews;

    private PullToRefreshListView mPtrListView;

    private LinearLayout mLinGoods;
    private LinearLayout mLinSight;

    private View mVTabLine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarType=1;
        super.onCreate(savedInstanceState);
        setMainContentView(R.layout.activity_my_collect);
        setBarTitle("我的收藏");
        initView();
    }




    private void initView(){

        mVTabLine = findViewById(R.id.v_tab_line);
        mLinGoods = (LinearLayout) findViewById(R.id.lin_goods);
//        mLinBlogs = (LinearLayout)findViewById(R.id.lin_blogs);
        mLinSight = (LinearLayout) findViewById(R.id.lin_sights);

        mLinGoods.setOnClickListener(this);
//        mLinBlogs.setOnClickListener(this);
        mLinSight.setOnClickListener(this);

//        mPtrListView = (PullToRefreshListView) findViewById(R.id.pull_listView);

        mViewPager = (ViewPager)findViewById(R.id.id_viewpager);

        mListViews = new ArrayList<View>();
        addView(mListViews, Constants.domain+"#/collectgoods");
        mPtrListView=new PullToRefreshListView(mContext);
        mListViews.add(mPtrListView);



        new ViewPaperUtil(this,mViewPager,false).bind(mListViews, new ViewPaperUtil.IGetCurrentSelectIndex() {
            @Override
            public void getSelectIndex(int pos) {
                if(pos==0){
                    calTabLinePosition(mLinGoods);
                }else {
                    calTabLinePosition(mLinSight);

                }
            }
        });

        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                calTabLinePosition(mLinGoods);
            }
        });


        FriendsManager.getMyFavouriteSight(mContext, Constants.userInfo.getAppUserId(), new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(String resonseData) {
                JSONArray jsonArray = parseObject(resonseData).getJSONArray("Result");
                mPtrListView.setAdapter(new FriendsListAdapter(jsonArray));
            }

            @Override
            public void onFailure(String resonseData) {

            }
        });

    }

    private void calTabLinePosition(ViewGroup vp) {
        LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mVTabLine.getLayoutParams();
        View son = (View) vp.getChildAt(0);
        lp.leftMargin = (int) (vp.getX() + son.getX());
        lp.width=son.getWidth();
        mVTabLine.setLayoutParams(lp);
    }

    private void addView(List<View> viewList, String url) {
        WebView webView = new WebView(this);
        ProductJsManager productJsManager=new ProductJsManager(this,webView);
        webView.addJavascriptInterface(productJsManager, "android");
        WebViewUtil.initWebView(this,webView);
        webView.loadUrl(url);
        viewList.add(webView);
    }


    @Override
    public void onClick(View v) {
        int vId = v.getId();

        switch (vId) {
            case R.id.lin_goods:
                mViewPager.setCurrentItem(0);
                calTabLinePosition(mLinGoods);
                break;
//            case R.id.lin_blogs:
//                mViewPager.setCurrentItem(1);
//                calTabLinePosition(mLinBlogs);
//                break;
            case R.id.lin_sights:
                mViewPager.setCurrentItem(2);
                calTabLinePosition(mLinSight);
                break;

        }
    }



    public class FriendsListAdapter extends BaseAdapter {
        private JSONArray mItems;

        public FriendsListAdapter(JSONArray jsonArray) {
            mItems = jsonArray;
        }

        @Override
        public int getCount() {
            return mItems.size();
        }

        @Override
        public JSONObject getItem(int position) {
            return mItems.getJSONObject(position - 1);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                convertView = inflater.inflate(R.layout.list_item_friends, null);
                holder = new ViewHolder();
                holder.imgUserLogo = (ImageView) convertView.findViewById(R.id.img_user_logo);
                holder.txtUserName = (TextView) convertView.findViewById(R.id.txt_user_name);
                holder.chCare = (CheckBox) convertView.findViewById(R.id.ch_care);
                holder.txtContent = (TextView) convertView.findViewById(R.id.txt_content);
                holder.imgLike = (ImageView) convertView.findViewById(R.id.img_like);
                holder.imgMessage = (ImageView) convertView.findViewById(R.id.img_message);
                holder.img = (ImageView) convertView.findViewById(R.id.img);
                holder.txtPostTime = (TextView) convertView.findViewById(R.id.txt_time);
                holder.txtLikeCount = (TextView) convertView.findViewById(R.id.txt_like_count);
                holder.holdView = convertView;
                holder.txtDelete = (TextView) convertView.findViewById(R.id.txt_delete);
                holder.fl_commit = (LinearLayout) convertView.findViewById(R.id.commit);
                ;
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final JSONObject jsonObject = mItems.getJSONObject(position);

//            final int sightId = jsonObject.getIntValue("SightId");

            holder.txtUserName.setText(jsonObject.getString("UserName"));

            holder.imgMessage.setTag(holder.fl_commit);




            holder.chCare.setVisibility(View.GONE);
            holder.imgMessage.setVisibility(View.GONE);

            Glide.with(mContext).load(jsonObject.getString("Pics")).into(holder.img);
            Glide.with(mContext).load(jsonObject.getString("HeadPic")).into(holder.imgUserLogo);
            holder.txtContent.setText(jsonObject.getString("Desc"));
            holder.txtPostTime.setText(jsonObject.getString("CreateTime"));
            holder.txtLikeCount.setText(jsonObject.getIntValue("UpVoteCount") + "次赞");
            holder.txtLikeCount.setTag(jsonObject.getIntValue("UpVoteCount"));
            holder.imgLike.setTag(null);
            holder.imgLike.setTag(holder.imgLike.getId(), holder.txtLikeCount);

            holder.imgLike.setVisibility(View.GONE);
//            ((View) holder.imgLike.getParent()).setTag(jsonObject.getIntValue("sightId"));

//            JSONArray jsonArray = jsonObject.getJSONArray("SightCommentInfo");
//
//            if (jsonArray != null) {
//                holder.fl_commit.removeAllViews();
//                for (int i = 0, l = jsonArray.size(); i < l; i++) {
//                    final JSONObject temp = jsonArray.getJSONObject(i);
//                    TextView textView = new TextView(mContext);
//                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                    lp.setMargins(0, 10, 0, 0);
//                    textView.setTextSize(14);
//                    textView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.selector_comt));
//                    textView.setTag(temp.getIntValue("VipId") + "," + sightId);
//                    textView.setTag(R.id.horizontal, holder.fl_commit);
////                    textView.setOnClickListener(new View.OnClickListener() {
////                        @Override
////                        public void onClick(View v) {
////                            mVpComt.setVisibility(View.VISIBLE);
////                            mVpComtContent = (ViewGroup) v.getTag(R.id.horizontal);
////                            String[] arr = v.getTag().toString().split(",");
////                            showSoftInput(mActivity, mVpComt, Integer.parseInt(arr[0]), Constants.userInfo.getAppUserId(), Integer.parseInt(arr[1]));
////                        }
////                    });
//                    textView.setText(temp.getString("CommentBody") + " " + temp.getString("CommentContent"));
//                    holder.fl_commit.addView(textView, lp);
//                }
//            }


            return convertView;
        }

        public void setItems(JSONArray list) {
            mItems = list;
            if (mItems == null) {
                mItems = new JSONArray();
            }
//            if (mItems.size() >= PAGE_SIZE) {
//                mPtrListView.setMode(Mode.BOTH);
//            } else {
//                mPtrListView.setMode(Mode.PULL_FROM_START);
//            }
            notifyDataSetChanged();
            mPtrListView.getRefreshableView().setSelected(true);
            mPtrListView.getRefreshableView().setSelection(0);
        }
//        public void attachItems(List<JSONObject> list) {
//            if (mItems == null) {
//                mItems = new ArrayList<JSONObject>();
//            }
//
//            if( list==null || list.size()==0){
//                mPtrListView.setMode(Mode.PULL_FROM_START);
//            }
//
//
//            if (list != null) {
//                mItems.addAll(list);
//                if (list.size() >= PAGE_SIZE) {
//                    mPtrListView.setMode(Mode.BOTH);
//                } else {
//                    mPtrListView.setMode(Mode.PULL_FROM_START);
//                }
//                notifyDataSetChanged();
//            }
//
//        }
    }

    public class ViewHolder {
        public ImageView imgUserLogo;
        public TextView txtUserName;
        public CheckBox chCare;
        public ImageView img;
        public TextView txtContent;
        public ImageView imgLike;
        public ImageView imgMessage;
        public TextView txtPostTime;
        public TextView txtDelete;
        public TextView txtLikeCount;
        public View holdView;
        public LinearLayout fl_commit;
    }



}